using System;
using System.Collections.Generic;
using System.Xml;
using System.Data;

namespace Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter
{
    /// <summary>
    /// Users class provides properties and methods to add and select users records.
    /// </summary>
    public class Users
    {
        Guid _userId = Guid.Empty;
        Guid _practiceId = Guid.Empty;
        string _userName = String.Empty;
        string _password = String.Empty;
        string _userXml;
        PracticeDBHandler _practiceDataSet = null;
        

        /// <summary>
        /// Constructor for users class.
        /// </summary>
        /// <param name="xmlFileName">Users xml file name.</param>
        public Users(string xmlFileName)
        {
            _userXml = xmlFileName;
            _practiceDataSet = new PracticeDBHandler(xmlFileName);
        }

        /// <summary>
        /// User id of the user
        /// </summary>
        public Guid UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        /// <summary>
        /// Practice id under which user is being added
        /// </summary>
        public Guid PracticeId
        {
            get { return _practiceId; }
            set { _practiceId = value; }
        }

        /// <summary>
        /// Username of the user
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>
        /// Password of the user
        /// </summary>
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        /// <summary>
        /// Adds new user in the User's xml file.
        /// </summary>
        public void AddUser()
        {
            DataRow[] parentPracticeRow = _practiceDataSet.SelectPractice("PracticeId = '" + this.PracticeId.ToString() + "'");
            _practiceDataSet.AddUsers(this.UserId, this.UserName, this.Password, parentPracticeRow[0]);
        }

        /// <summary>
        /// Gets list of users records according to the conditions
        /// </summary>
        /// <param name="condition">Condition to select records</param>
        /// <returns>List of Users records</returns>
        public List<Users> SelectUsersRecords(string condition)
        {
            string sortColumnName = String.Empty;
            Users user = null;
            DataRow[] usersRows = _practiceDataSet.SelectUsers(condition);
            List<Users> usersList = new List<Users>();
            for (int counter = 0; counter < usersRows.Length; counter++)
            {
                user = new Users(this._userXml);
                user.UserId = (Guid)usersRows[counter]["userid"];
                user.PracticeId = (Guid)usersRows[counter]["PracticeId"];
                user.UserName = usersRows[counter]["username"].ToString();
                user.Password = usersRows[counter]["password"].ToString();
                usersList.Add(user);
            }
            return usersList;
        }
    }
}